package com.intellagric.service.impl.module.sys_menu;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.SysMenuMapper;
import com.intellagric.pojo.SysMenu;
import com.intellagric.pojo.SysMenuExample;
import com.intellagric.service.module.sys_menu.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @Auther: zhy
 * @Date: 2018/10/4
 * @Description: 菜单管理服务
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 添加菜单
     * @Param sysMenu
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addMenu(SysMenu sysMenu) {
        if (sysMenuMapper.insertSelective(sysMenu) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除菜单
     * @Param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteMenu(String id) {
        if (sysMenuMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改菜单
     * @Param sysMenu
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editMenu(SysMenu sysMenu) {
        if (sysMenuMapper.updateByPrimaryKeySelective(sysMenu) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    @Override
    public SysMenu getMenuById(String id) {
        return sysMenuMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询菜单数据信息，生成树形菜单
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getMenu() {
        //查询所有列表数据
        LayuiDataGridResult result = new LayuiDataGridResult();
        List<SysMenu> menuList = this.getMenuList().getData();
        result.setData(menuList);

        //查询顶级菜单
        List<SysMenu> rootSysMenuList = this.getMenuByParentId("0").getData();

        //生成树形菜单数据，为一级菜单设置子菜单，getChild是递归调用的
        for (SysMenu sysMenu : rootSysMenuList) {
            sysMenu.setChildren(getChild(sysMenu.getId(), menuList));
        }
        result.setData(rootSysMenuList);
        return result;
    }

    private List<SysMenu> getChild(String id,List<SysMenu> menuList) {
        // 子菜单
        List<SysMenu> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (SysMenu sysMenu : menuList) {
            if (sysMenu.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (sysMenu.getParentId().equals(id)) {
                    childList.add(sysMenu);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (SysMenu sysMenu : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            sysMenu.setChildren(getChild(sysMenu.getId(), menuList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    /**
     * 异步查询菜单，通过父id查询
     * @Param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getMenuByParentId(String parentId) {
        SysMenuExample sysMenuExample = new SysMenuExample();
        sysMenuExample.setOrderByClause("sort DESC");//以sort字段进行排序
        sysMenuExample.createCriteria().andParentIdEqualTo(parentId);
        List<SysMenu> rootSysMenuList = sysMenuMapper.selectByExample(sysMenuExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(rootSysMenuList);
        return result;
    }

    /**
     * 查询菜单列表信息
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getMenuList() {
        SysMenuExample sysMenuExample = new SysMenuExample();
        sysMenuExample.setOrderByClause("sort DESC");//以sort字段进行排序
        sysMenuExample.createCriteria().andIdIsNotNull();
        List<SysMenu> sysMenuList = sysMenuMapper.selectByExample(sysMenuExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(sysMenuList);
        return result;
    }



}
